from threading import Thread, active_count
from time import sleep

import string, random
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.chrome.options import Options
from rich import print as rprint

class Browser:
    def __init__(self):
        options = Options()
        options.page_load_strategy = 'eager'
        options.add_argument('--start-maximized')
        options.add_argument('--incognito')
        self.browser = webdriver.Chrome(options=options)

    def input_content(self, id, content=True):
        if content:
            content = ''
            for i in random.choices(string.ascii_letters, k=10):
                content += i
        else:
            content = 'https://www.vzone.pw/auth/register'
        self.browser.find_element(By.ID, id).send_keys(content)

    def regester(self, url):
        while True:
            try:
                self.browser.get(url)
                sleep(1)
                break
            except:
                continue
        Browser.input_content(self, 'name')
        Browser.input_content(self, 'email')
        Browser.input_content(self, 'passwd', content=False)
        Browser.input_content(self, 'repasswd', content=False)
        try:
            WebDriverWait(self.browser,40).until(EC.presence_of_element_located((By.CLASS_NAME, 'geetest_radar_tip')))      # 等待滑动验证
            self.browser.find_element(By.CLASS_NAME, 'geetest_radar_tip').click()
            WebDriverWait(self.browser,30).until(EC.text_to_be_present_in_element((By.CLASS_NAME, 'geetest_success_radar_tip_content'), '验证成功'))
            self.browser.find_element(By.ID, 'register-confirm').click()
            WebDriverWait(self.browser,20).until(EC.presence_of_element_located((By.XPATH, '//div[@class="swal2-actions"]/button')))     # 等待注册成功
            self.browser.find_element(By.XPATH, '//div[@class="swal2-actions"]/button').click()
        except:
            self.regester(url)        

    def first_reg(self):
        Browser.regester(self, 'https://www.recear.co/auth/register')
        WebDriverWait(self.browser,30).until(EC.title_is('首页 — RE'))
        self.browser.execute_script('window.scrollTo(0,500)')
        global subscribe_link
        subscribe_link = self.browser.find_element(By.XPATH, '//div[@class="buttons"]/a[5]').get_attribute('data-clipboard-text')
        rprint('[green]{}\n订阅链接：{}\n{}'.format('-'*110, subscribe_link, '-'*110))
        self.browser.get('https://www.recear.co/user/invite')
        while True:
            try:
                invite_link = self.browser.find_element(By.XPATH, '//div[@class="mt-4"]/a').get_attribute('data-clipboard-text')
                break
            except:
                self.browser.back()
                continue
        self.browser.close()
        return invite_link

invite_link = Browser().first_reg()

for i in range(2):
    Thread(target=Browser().regester, args=(invite_link,)).start()
    rprint(f'已邀请 1 个账号注册，累计邀请 {i+1} 个')

while True:
    if active_count() == 1:
        break

rprint('[yellow]邀请完成', )
rprint('邀请链接：', invite_link)
rprint('[green]{}\n订阅链接：{}\n{}'.format('-'*110, subscribe_link, '-'*110))
next = input('回车关闭窗口')

